home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / MDB2 / LOB.php < prev   
Encoding:
PHP Script  |  2006-04-07  |  7.6 KB  |  251 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: LOB.php,v 1.24 2006/02/03 19:13:04 lsmith Exp $
  46.  
  47. /**
  48.  * @package  MDB2
  49.  * @category Database
  50.  * @author   Lukas Smith <smith@pooteeweet.org>
  51.  */
  52.  
  53. require_once 'MDB2.php';
  54.  
  55. /**
  56.  * MDB2_LOB: user land stream wrapper implementation for LOB support
  57.  *
  58.  * @package MDB2
  59.  * @category Database
  60.  * @author Lukas Smith <smith@pooteeweet.org>
  61.  */
  62. class MDB2_LOB
  63. {
  64.     /**
  65.      * contains the key to the global MDB2 instance array of the associated
  66.      * MDB2 instance
  67.      *
  68.      * @var integer
  69.      * @access protected
  70.      */
  71.     var $db_index;
  72.  
  73.     /**
  74.      * contains the key to the global MDB2_LOB instance array of the associated
  75.      * MDB2_LOB instance
  76.      *
  77.      * @var integer
  78.      * @access protected
  79.      */
  80.     var $lob_index;
  81.  
  82.     /**
  83.      * LOB data
  84.      *
  85.      * @var string
  86.      * @access protected
  87.      */
  88.     var $lob;
  89.  
  90.     // {{{ stream_open()
  91.  
  92.     /**
  93.      * open stream
  94.      *
  95.      * @param string specifies the URL that was passed to fopen()
  96.      * @param string the mode used to open the file
  97.      * @param int holds additional flags set by the streams API
  98.      * @param string not used
  99.      *
  100.      * @return bool
  101.      * @access public
  102.      */
  103.     function stream_open($path, $mode, $options, &$opened_path)
  104.     {
  105.         if (!preg_match('/^rb?\+?$/', $mode)) {
  106.             return false;
  107.         }
  108.         $url = parse_url($path);
  109.         if (!array_key_exists('host', $url) && !array_key_exists('user', $url)) {
  110.             return false;
  111.         }
  112.         $this->db_index = $url['host'];
  113.         if (!isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
  114.             return false;
  115.         }
  116.         $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  117.         $this->lob_index = $url['user'];
  118.         if (!isset($db->datatype->lobs[$this->lob_index])) {
  119.             return false;
  120.         }
  121.         $this->lob =& $db->datatype->lobs[$this->lob_index];
  122.         $db->datatype->_retrieveLOB($this->lob);
  123.         return true;
  124.     }
  125.     // }}}
  126.  
  127.     // {{{ stream_read()
  128.  
  129.     /**
  130.      * read stream
  131.      *
  132.      * @param int number of bytes to read
  133.      *
  134.      * @return string
  135.      * @access public
  136.      */
  137.     function stream_read($count)
  138.     {
  139.         if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
  140.             $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  141.  
  142.             $data = $db->datatype->_readLOB($this->lob, $count);
  143.             $length = strlen($data);
  144.             if ($length == 0) {
  145.                 $this->lob['endOfLOB'] = true;
  146.             }
  147.             $this->lob['position'] += $length;
  148.             return $data;
  149.         }
  150.     }
  151.     // }}}
  152.  
  153.     // {{{ stream_write()
  154.  
  155.     /**
  156.      * write stream, note implemented
  157.      *
  158.      * @param string data
  159.      *
  160.      * @return int
  161.      * @access public
  162.      */
  163.     function stream_write($data)
  164.     {
  165.         return 0;
  166.     }
  167.     // }}}
  168.  
  169.     // {{{ stream_tell()
  170.  
  171.     /**
  172.      * return the current position
  173.      *
  174.      * @return int current position
  175.      * @access public
  176.      */
  177.     function stream_tell()
  178.     {
  179.         return $this->lob['position'];
  180.     }
  181.     // }}}
  182.  
  183.     // {{{ stream_eof()
  184.  
  185.     /**
  186.      * check if stream reaches EOF
  187.      *
  188.      * @return bool
  189.      * @access public
  190.      */
  191.     function stream_eof()
  192.     {
  193.         if (!isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
  194.             return true;
  195.         }
  196.         $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  197.         $result = $db->datatype->_endOfLOB($this->lob);
  198.         if (version_compare(phpversion(), "5.0", ">=")
  199.             && version_compare(phpversion(), "5.1", "<")
  200.         ) {
  201.             return !$result;
  202.         }
  203.         return $result;
  204.     }
  205.     // }}}
  206.  
  207.     // {{{ stream_seek()
  208.  
  209.     /**
  210.      * seek stream, not implemented
  211.      *
  212.      * @param int offset
  213.      * @param int whence
  214.      *
  215.      * @return bool
  216.      * @access public
  217.      */
  218.     function stream_seek($offset, $whence)
  219.     {
  220.         return false;
  221.     }
  222.     // }}}
  223.  
  224.     // {{{ stream_close()
  225.  
  226.     /**
  227.      * close stream
  228.      *
  229.      * @access public
  230.      */
  231.     function stream_close()
  232.     {
  233.         if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
  234.             $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  235.             if (isset($db->datatype->lobs[$this->lob_index])) {
  236.                 $db->datatype->_destroyLOB($this->lob_index);
  237.                 unset($db->datatype->lobs[$this->lob_index]);
  238.             }
  239.         }
  240.     }
  241.     // }}}
  242. }
  243.  
  244. // register streams wrapper
  245. if (!stream_wrapper_register("MDB2LOB", "MDB2_LOB")) {
  246.     MDB2::raiseError();
  247.     return false;
  248. }
  249.  
  250. ?>
  251.